package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.course.CourseVideo;
import com.hualan.sfzy.service.course.CourseVideoService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CourseVideoController {
    @Autowired
    private CourseVideoService courseVideoService;

    @PostMapping("/courseVideo")
    public Result add(@RequestBody CourseVideo courseVideo) {
        return courseVideoService.add(courseVideo) > 0 ? Result.success() : Result.error();
    }

    @PutMapping("/courseVideo")
    public Result edit(@RequestBody CourseVideo courseVideo) {
        return courseVideoService.edit(courseVideo) > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/courseVideo")
    public Result remove(Integer[] ids) {
        return courseVideoService.removeBatch(ids) > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/courseVideo/{id}")
    public Result remove(@PathVariable Integer id) {
        return courseVideoService.remove(id) > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/courseVideo")
    public Result find() {
        List<CourseVideo> courseVideoList = courseVideoService.find();
        return Result.success(courseVideoList);
    }

    @GetMapping("/courseVideo/{id}")
    public Result findById(@PathVariable Integer id) {
        CourseVideo courseVideo = courseVideoService.findById(id);
        return Result.success(courseVideo);
    }

    @GetMapping("/courseVideo/page")
    public Object findByPage(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false)
            Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5", required = false)
            Integer pageSize,
            CourseVideo courseVideo) {
        Map<String, Object> map = courseVideoService.findByPage(pageNum, pageSize, courseVideo);
        return Result.success(map);
    }
}